<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>惰性兼容封装</title>
</head>
<body>
    <div class="box">点击我</div>

    <script>
        var oBox = document.getElementsByClassName("box")[0];

        var fun = addEvent(oBox,"click",function(){
            console.log(this);  //oBox元素
        });
        console.log(fun); //addEventListener


        function addEvent(el,type,fn,capture){
            if(el.addEventListener){
                addEvent = function(el,type,fn,capture){
                    el.addEventListener(type,fn,capture);
                    return 'addEventListener';
                }
            }else if(el.attachEvent){
                addEvent = function(el,type,fn){
                    el.attachEvent('on'+type,fn);
                    return 'attachEvent';
                }
            }else{
                addEvent = function(el,type,fn){
                    el['on'+type] = fn;
                    return 'on'
                }
            }
            return addEvent(el,type,fn,capture);
        }

    </script>
</body>
</html>
